package com.cgwx.Utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpUtil {

    // http请求
    public static String httpRequest(String url) {

        String resultJson = "";
        try {
            HttpClient httpClient = new HttpClient( );
            httpClient.getParams().setContentCharset( "UTF-8" );
            PostMethod postMethod = new PostMethod( url );
            RequestEntity requestEntity = new StringRequestEntity("", "application/xml", "UTF-8");
            postMethod.setRequestEntity(requestEntity);
            //postMethod.setRequestBody("");
            //postMethod.setParameter("id", userId);
            httpClient.executeMethod(postMethod);
            resultJson = new String(postMethod.getResponseBody());
        } catch (Exception e) {
            e.printStackTrace();
            resultJson = "Exception:com.cgwx.aop.util.HttpUtil.httpGet";
        }

        return resultJson;
    }


    // http请求带requestbody
    public static String httpRequestBody(String url, String userInfo, String fileFolders, String zipName) {
        String resultJson = "";
        try {
            HttpClient httpClient = new HttpClient( );
            httpClient.getParams().setContentCharset( "UTF-8" );
            PostMethod postMethod = new PostMethod( url );
            RequestEntity requestEntity = new StringRequestEntity("Content-Type", "application/xml", "UTF-8");
            postMethod.setRequestEntity(requestEntity);
            postMethod.setParameter("fileFoldersJson", fileFolders);
            postMethod.setParameter("zipName", zipName);
            postMethod.setParameter("userInfoJson", userInfo);
            httpClient.executeMethod(postMethod);
            resultJson = new String(postMethod.getResponseBody());
        } catch (Exception e) {
            e.printStackTrace();
            resultJson = "Exception:com.cgwx.aop.util.HttpUtil.httpGet";
        }
        return resultJson;
    }

    // httpClientGet请求
    public static String HttpClientGet(String url) throws Exception {

        // 获取http客户端
        CloseableHttpClient client = HttpClients.createDefault();
        // 通过httpget方式来实现我们的get请求
        HttpGet httpGet = new HttpGet(url);
        // 通过client调用execute方法，得到我们的执行结果就是一个response，所有的数据都封装在response里面了
        CloseableHttpResponse Response = client.execute(httpGet);
        // HttpEntity
        // 是一个中间的桥梁，在httpClient里面，是连接我们的请求与响应的一个中间桥梁，所有的请求参数都是通过HttpEntity携带过去的
        // 所有的响应的数据，也全部都是封装在HttpEntity里面
        HttpEntity entity = Response.getEntity();
        // 通过EntityUtils 来将我们的数据转换成字符串
        String str = EntityUtils.toString(entity, "UTF-8");
        // 关闭
        Response.close();
        return str;
    }

/*    public static String httpGet(String url, String charset) {

        String resultJson = "";
        HttpGet httpGet = new HttpGet();
        // 设置参数
        try {
            HttpClient httpClient = new HttpClient( );
            httpGet.setURI(new URI(url));
            // 发送请求
            HttpResponse httpResponse = httpClient.executeMethod(httpGet);
            // 获取返回的数据
            HttpEntity entity = httpResponse.getEntity();
            byte[] body = EntityUtils.toByteArray(entity);
            StatusLine sL = httpResponse.getStatusLine();
            int statusCode = sL.getStatusCode();
            if (statusCode == 200) {
                resultJson = new String(body, charset);
                entity.consumeContent();
            } else {
                resultJson = "Exception:com.cgwx.aop.util.HttpUtil.httpGet:statusCode="+statusCode;
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultJson = "Exception:com.cgwx.aop.util.HttpUtil.httpGet:"+e.getMessage();
        }

        return resultJson;
    }*/

}
